package com.qc31.comlib.map

import com.qc31.baselib.utils.CacheMMKV
import com.qc31.baselib.utils.GsonUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/** @Author Jasper @Time 2020/9/14 @Desc Marker帮助类 */
object MarkerSet {

    private const val KEY_MARKERSET = "MarkerSet"

    var mMarkerSet: MapMarker = MapMarker()

    //字体颜色
    private var _textColor = "#FFFFFF"

    //车牌背景色
    private var _backCarColor = "#FF0024"

    //速度背景色
    private var _backSpeedColor = "#D7A675"
    private val markerSub = PublishSubject.create<MapMarker>()

    fun mapMarkerObserver(): Observable<MapMarker> = markerSub.hide()

    private val local = CacheMMKV.decodeString(KEY_MARKERSET)

    val localSet = if (local.isEmpty()) MapMarker() else GsonUtil.formObject(local)


    init {
        initMarker(null)
    }

    /** 初始化设置数据 */
    private fun initMarker(markerSet: MapMarker?) {
        if (markerSet != null) mMarkerSet = markerSet
        else {
            setMapMarker(localSet, mMarkerSet)
        }
        markerSet?.let {
            setMapMarker(it, localSet)
            _textColor = it.textColor
            _backCarColor = it.backCarColor
            _backSpeedColor = it.backSpeedColor
        }
        markerSub.onNext(localSet)
    }

    private fun setMapMarker(fromMarker: MapMarker, toMarker: MapMarker) {
        fromMarker.run {
            toMarker.textSize = textSize
            toMarker.iconSize = iconSize
            toMarker.backCarColor = backCarColor
            toMarker.backSpeedColor = backSpeedColor
            toMarker.colorType = colorType
            toMarker.showPlate = showPlate
            toMarker.showSpeed = showSpeed
            toMarker.textColor = textColor
        }
    }

    /** 颜色菜单选择 */
    fun changeColorType(type: Int) {
        localSet.colorType = type
        when (type) {
            1 -> {
                localSet.textColor = "#FFFFFF"
                localSet.backCarColor = "#3791E9"
                localSet.backSpeedColor = "#EA4354"
            }
            2 -> {
                localSet.textColor = "#333333"
                localSet.backCarColor = "#7AC668"
                localSet.backSpeedColor = "#FFCC00"
            }
            3 -> {
                localSet.textColor = "#333333"
                localSet.backCarColor = "#FFA200"
                localSet.backSpeedColor = "#99D5FE"
            }
            else -> {
                localSet.textColor = _textColor
                localSet.backCarColor = _backCarColor
                localSet.backSpeedColor = _backSpeedColor
            }
        }
        markerSub.onNext(localSet)
    }

    /** 设置是否只展示车牌 */
    fun showPlateName(isPlate: Boolean) {
        localSet.showPlate = isPlate
    }

    /** 设置是否展示速度 */
    fun showSpeed(isSpeed: Boolean) {
        localSet.showSpeed = isSpeed
    }

    /** 设置字体颜色 */
    fun setTextColor(textColor: String) {
        localSet.textColor = textColor
        _textColor = textColor
    }

    /** 设置车牌背景 */
    fun setPlateColor(plateColor: String) {
        localSet.backCarColor = plateColor
        _backCarColor = plateColor
    }

    /** 设置速度背景色 */
    fun setSpeedColor(speedColor: String) {
        localSet.backSpeedColor = speedColor
        _backSpeedColor = speedColor
    }

    /** 设置图标大小 */
    fun setIconSize(iconSize: Int) {
        localSet.iconSize = iconSize
    }

    /** 设置图标字体大小 */
    fun setTextSize(textSize: Int) {
        localSet.textSize = textSize
    }


    /** 保存数据到后台 */
    fun saveMarkerSet(): String {
        setMapMarker(localSet, mMarkerSet)
        val toJson = GsonUtil.toJson(localSet)
        CacheMMKV.encode(KEY_MARKERSET, toJson)
        return toJson
    }

    fun reset() {
        setMapMarker(mMarkerSet, localSet)
        mMarkerSet.let {
            _textColor = it.textColor
            _backCarColor = it.backCarColor
            _backSpeedColor = it.backSpeedColor
        }
        markerSub.onNext(localSet)
    }



}